/*          ._
 *            '.			     _______ _______ ___ ___ _______ 
 *     --._     \    .-.		|   |   |_     _|   |   |    ___|
 *         '. .--;  /  |		|       |_|   |_|   |   |    ___|
 *         ,;/ ^  |`\  /.-"".   |___|___|_______|\_____/|_______|	
 *        ;' \  _/   |'    .'          code.google.com/p/as-hive
 *       /    `.;I> /_.--.` )    __
 *      |    .'`  .'( _.Y/`;    /  \__
 *       \_.'---'`   `\  `-/`-. \__/  \__
 *         /_-.`-.._ _/`\  ;-./_/  \__/  \
 *        |   -./  ;.__.'`\  /  \__/  \__/
 *         `--'   (._ _.'`|  \__/  \__/
 *                /     ./ __/  \__/  \
 *           jgs ; `--';'_/  \__/  \__/
 *               `;-,-'/  \__/  \
 *               ,'`   \__/  \__/
 *
 * Copyright 2009 (c) Jovan Alleyne, Peter Nitsch, Brandon Flowers.
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

package org.hive.managers
{
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.utils.Dictionary;
	
	import org.hive.Hive;
	import org.hive.events.*;
	import org.hive.sequence.*;
	import org.hive.state.*;

    /**
    *
    */
	public class StateManager extends Manager
	{
		//________________________________________________ PROPERTIES

		public var currentState : State;
        public var nextState : State;
		public static var documentClass	: DisplayObjectContainer;


        private static var __stateCloseEvent : Event;
        private static var __activeSequenceDelays : Dictionary = new Dictionary( false );
		private static var __cancelList : Dictionary=new Dictionary();

		
		
		//________________________________________________ CONSTRUCTOR

		
		public function StateManager( hive:Hive )
		{
			super(hive);
		}
	

		//________________________________________________ PUBLIC METHODS
		
        /**
        *
        * @param	newState		Instance of application state to switch to.
        * @param	e				Event instance that caused state switch.
        * @return
        */
		public function switchState( newState:State, e:Event=null ):void
		{
			if(Hive.DEBUG_MODE){trace('CoreManager :: switchState')}

            nextState = newState;

            __stateCloseEvent = e;
           
            if(currentState){
            	var closeSequence:CloseSequence = IState(currentState).close( );
	            hive.sequenceManager.playSequence( closeSequence );
            }else{
            	executeStateSwitch( );
            }
		}


        /**
        *
        * @param	state
        * @return
        */
		private function configState( state:State ):State
		{
			state.hive = hive;
			state.stateManager = hive.stateManager;
            state.sequenceManager = hive.sequenceManager;

			return state;
		}

        /**
        *
        * @return
        */
		internal function executeStateSwitch( ):ActionSequence
        {
            if( currentState ) 
            currentState.previousState = null; // TODO: possibly add depthlimit so users can determine how far back to remember states
            
            var ls:State = currentState;
            currentState = configState( nextState );

            nextState = null;
			
			currentState.previousState 	 = ls;
			currentState.trigger 		 = __stateCloseEvent;
            
			dispatchEvent( new StateManagerEvent(StateManagerEvent.ON_STATE_SWITCHED, currentState.previousState, currentState) );
			
			var sequence:ActionSequence = IState(currentState).create( );
			hive.sequenceManager.playSequence( sequence );

            return sequence;
        }
		
	}
}